package com.hy.controller.property;


import cn.hutool.core.bean.BeanUtil;

import com.hy.common.base.RespResult;
import com.hy.common.excel.EntityExcelExport;


import com.hy.common.log.annotation.SysOpLogAnnotation;
import com.hy.entity.dto.HyPropertySearchDTO;
import com.hy.entity.dto.PrinterPropertySearchDto;
import com.hy.entity.dto.RecordDTO;
import com.hy.entity.pojo.HyProperty;

import com.hy.entity.vo.HyPropertyVO;
import com.hy.service.HyPropertyRecordService;
import com.hy.service.HyPropertyService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/property")
public class HyPropertyController {


    @Resource
    private HyPropertyService entityService;

    @Resource
    private EntityExcelExport entityExcelExport;
    @Resource
    private HyPropertyRecordService propertyRecordService;

    // 导出小数据量数据到excel
    @RequestMapping("/exportExcelSmallData")
    @SysOpLogAnnotation(menuName = "资产",menuBtn = "导出资产",reqParam = "'按条件导出'")
    public void exportExcelSmallData(HttpServletResponse response,HyPropertySearchDTO hyPropertySearchDTO){

        entityExcelExport.exportWithSmallData(response, "资产库列表", hyPropertySearchDTO);
    }

    // 从数据库中导出百万数据到excel
    @RequestMapping("/exportExcelBigData")
    @SysOpLogAnnotation(menuName = "资产",menuBtn = "导出资产",reqParam = "'按条件导出'")
    public void exportExcelBigData(HttpServletResponse response , HyPropertySearchDTO hyPropertySearchDTO){
        hyPropertySearchDTO.setDel(1);
//        hyPropertySearchDTO.setOutProperty(1);
        entityExcelExport.exportWithBigData(response, "资产库列表",hyPropertySearchDTO);
    }

    // 从excel读取百万数据导入到数据库
    @RequestMapping("/importExcelBigData")
    @SysOpLogAnnotation(menuName = "资产",menuBtn = "导入资产",reqParam = "'资产导入'")
    public RespResult importExcelBigData(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request){


        return  entityService.importExcelBigData(multipartFile,request);
    }


    @SysOpLogAnnotation(menuName = "资产",menuBtn = "下载导入模板",reqParam = "'下载模板'")
    @RequestMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response){

       entityService.downloadTemplate(response) ;
    }

    @GetMapping("/showPropertyMsg")
    @SysOpLogAnnotation(menuName = "资产",menuBtn = "查询资产",reqParam = "'查询条件'")
    public RespResult showPropertyMsg(HyPropertySearchDTO searchDTO){



//        searchDTO.setDel(1);
//        searchDTO.setOutProperty(1);
        List<HyPropertyVO> propertyVOS= entityService.listPageByDto(BeanUtil.beanToMap(searchDTO),searchDTO.getCurrentPage(),searchDTO.getPageSize());

        return RespResult.success(propertyVOS).setCount(entityService.getCount(searchDTO));
    }

    @PostMapping("/updateProperty")
    @SysOpLogAnnotation(menuName = "资产",menuBtn = "修改资产",reqParam = "'修改资产：'+#hyProperty.propertyId")
    public  RespResult update(@RequestBody HyProperty hyProperty){
        if (hyProperty.getDel().equals(2)){
            return RespResult.error(500,"不存在的资源");
        }
        return  entityService.updateProperty(hyProperty);
    }
    @SysOpLogAnnotation(menuName = "资产",menuBtn = "添加资产",reqParam = "'添加资产: '+#hyProperty.propertyId")
    @PostMapping("/add")
    public RespResult add(@RequestBody HyProperty hyProperty){

        return entityService.handAdd(hyProperty);


    }

    @PostMapping("/del")
    @SysOpLogAnnotation(menuName = "资产",menuBtn = "删除资产",reqParam = "'删除资产: '+#ids")
    public RespResult del(Integer[] ids){
        List<Integer> integers=new ArrayList<>();
        Collections.addAll(integers,ids);
        return entityService.delete(integers);


    }

    @RequestMapping("/getCheckRecord")
    @SysOpLogAnnotation(menuName = "资产",menuBtn = "查看一个资产的变更记录",reqParam = "'资产ID: '+#propertyId")
    public RespResult getCheckRecord(Integer propertyId){


        return propertyRecordService.getOnePropertyRecord(propertyId);
    }
    @PostMapping("/showAllPropertyRecord")
    @SysOpLogAnnotation(menuName = "资产",menuBtn = "查看所有资产变更记录",reqParam = "'查询条件'")
    public RespResult showAllPropertyRecord(@RequestBody RecordDTO recordDTO){

        return propertyRecordService.showAllPropertyRecord(recordDTO);
    }

    @SysOpLogAnnotation(menuName = "资产",menuBtn = "打印程序获取资产列表",reqParam = "'打印")
    @PostMapping("/printGet")
    public RespResult printGet(PrinterPropertySearchDto searchDto){


        searchDto.setDel(1);
//        searchDto.setOutProperty(1);
        return entityService.printGet(searchDto);
    }

    @RequestMapping("/checkMsg")
    @SysOpLogAnnotation(menuName = "公共",menuBtn = "同意资产变更",reqParam = "'同意了'+#recordId")
    public RespResult checkMsg(Integer recordId){



        return entityService.checkMsg(recordId);

    }
    @RequestMapping("/showWaitReadApply")
    @SysOpLogAnnotation(menuName = "公共",menuBtn = "查看待处理变更申请",reqParam = "'查看'")
    public RespResult showWaitReadApply(){


        return propertyRecordService.showWaitReadApply();

    }
}
